package com.study.cloud.gateway.common.response;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.study.cloud.gateway.common.enums.ResponseCode;
import com.study.cloud.gateway.common.util.JSONUtil;
import io.netty.handler.codec.http.*;
import lombok.Data;
import org.asynchttpclient.Response;

/**
 * @author wangwancheng
 * @create 2025/7/17 15:40
 */
@Data
public class GatewayResponse {
    //响应头
    private HttpHeaders responseHeaders = new DefaultHttpHeaders();
    //额外响应头
    private final HttpHeaders extraResponseHeaders = new DefaultHttpHeaders();
    //返回响应内容
    private String content;
    //返回响应的状态码
    private HttpResponseStatus httpResponseStatus;

    //如果是异步的化，还需要一个异步返回的对象
    private Response futureResponse;

    public void putHead(CharSequence key,CharSequence var){
        responseHeaders.add(key,var);
    }

    /**
     * 构建响应式访问对象 （异步对象）
     * @param futureResponse
     * @return
     */
    public static GatewayResponse build(Response futureResponse){
        GatewayResponse gatewayResponse = new GatewayResponse();
        gatewayResponse.setFutureResponse(futureResponse);
        gatewayResponse.setContent(futureResponse.getResponseBody());
        gatewayResponse.setHttpResponseStatus(HttpResponseStatus.valueOf(futureResponse.getStatusCode()));
        return gatewayResponse;
    }

    public static GatewayResponse build(String content,HttpResponseStatus httpResponseStatus){
        GatewayResponse gatewayResponse = new GatewayResponse();
        gatewayResponse.setContent(content);
        gatewayResponse.setHttpResponseStatus(httpResponseStatus);
        return gatewayResponse;
    }

    /**
     * 返回一个json类型的响应信息，这里是失败的时候使用
     * @param code
     * @param args
     * @return
     */
    public static GatewayResponse build(ResponseCode code, Object... args){
        ObjectNode objectNode = JSONUtil.createObjectNode();
        objectNode.put(JSONUtil.STATUS,code.getStatus().code());
        objectNode.put(JSONUtil.CODE,code.getCode());
        objectNode.put(JSONUtil.MESSAGE,code.getMessage());
        GatewayResponse gatewayResponse = new GatewayResponse();
        gatewayResponse.setHttpResponseStatus(code.getStatus());
        gatewayResponse.putHead(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON+";charset=utf-8");
        gatewayResponse.setContent(JSONUtil.toJSONString(objectNode));
        return gatewayResponse;
    }

    /**
     * 返回一个json类型的响应信息，这里是成功时候使用
     * @param data
     * @return
     */
    public static GatewayResponse build(Object data){
        ObjectNode objectNode = JSONUtil.createObjectNode();
        objectNode.put(JSONUtil.STATUS,ResponseCode.SUCCESS.getStatus().code());
        objectNode.put(JSONUtil.CODE,ResponseCode.SUCCESS.getCode());
        objectNode.putPOJO(JSONUtil.DATA,data);
        GatewayResponse gatewayResponse = new GatewayResponse();
        gatewayResponse.setHttpResponseStatus(ResponseCode.SUCCESS.getStatus());
        gatewayResponse.putHead(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON+";charset=utf-8");
        gatewayResponse.setContent(JSONUtil.toJSONString(objectNode));
        return gatewayResponse;
    }

}
